//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#define kTableViewReuse @"cellReuse"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *nameArray;
@property (nonatomic,strong) UITableView *nameTableView;
@property (nonatomic,strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
//选中名字的数组
@property (nonatomic,strong) NSMutableArray<NSString *> *selectNameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.nameTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
   NSArray *array = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    
    _selectIndexArray = [NSMutableArray array];
    _selectNameArray = [NSMutableArray array];
    self.nameArray = [NSMutableArray arrayWithArray:array];
    _nameTableView.delegate = self;
    _nameTableView.dataSource = self;
    [self.view addSubview:_nameTableView];
    [_nameTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewReuse];
//    设置右barButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    NSLog(@"%@",NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
    
}
#pragma mark - tableView DataSource
//2
//让tableview进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_nameTableView setEditing:editing animated:animated];
//    [_nameTableView setEditing:YES animated:YES];
    if (editing) {
        NSLog(@"编辑");
    } else {
//        要删除选中的数组
//        [_nameArray removeObjectsInArray:_selectNameArray];
//        删除cell
//        删除数据源
//        [_nameArray removeObjectsInArray:_selectNameArray];
//        [_nameTableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationRight];
//        [_selectNameArray removeAllObjects];
//        [_selectIndexArray removeAllObjects];
        
        NSLog(@"完成");
    }
}

- (void)editAction
{
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    
    if (self.editing == NO) {
        [_nameArray removeObjectsInArray:_selectNameArray];
        [_nameTableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationTop];
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
        

    }
}


//2
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//3
//默认是delegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleInsert |  UITableViewCellEditingStyleDelete;
//    return UITableViewCellEditingStyleNone;
//    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editing) {
//        在全选状态下，数组中添加对应选中的下标
//        取出对应下标的名字
        NSString *name = _nameArray[indexPath.row];
        [_selectNameArray addObject:name];
        
        [_selectIndexArray addObject:indexPath];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editing) {
//         在全选状态下，数组中删除对应选中的下标
        NSString *name = _nameArray[indexPath.row];
        [_selectNameArray removeObject:name];
//        [_selectNameArray removeObjectsInArray:name];
        [_selectIndexArray removeObject:indexPath];
    }
}

//4
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
#warning     如果对tableview进行添加删除，先修改数据源，在进行添加或删除
//        修改数据源
        [_nameArray removeObjectAtIndex:indexPath.row];
//        对应修改cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        NSLog(@"插入"
              
              );
        
        [_nameArray insertObject:@"发发" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
//    tableView不论删除还是添加，都已经进行了刷新操作
}
//5
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _nameArray.count;
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
    
    
}

//- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}

#pragma mark - table Datasource
//默认可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//移动完成，更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
{
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    NSLog(@"%@",_nameArray);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
